#include "surfaceMgrPanel.h"
#include "glWidget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>

surfaceMgrPanel::surfaceMgrPanel(QWidget* parent)
	: QFrame(parent)
{
	QVBoxLayout* mainLayout = new QVBoxLayout(this);

	QHBoxLayout* hLayout = new QHBoxLayout();
	hLayout->setContentsMargins(0, 0, 0, 0);
	QPushButton* addBtn = new QPushButton("add");
	connect(addBtn, &QPushButton::clicked, this, &surfaceMgrPanel::onAddBtnClicked);
	QPushButton* delBtn = new QPushButton("del");
	connect(delBtn, &QPushButton::clicked, this, &surfaceMgrPanel::onDelBtnClicked);
	QPushButton* editBtn = new QPushButton("edit");
	connect(editBtn, &QPushButton::clicked, this, &surfaceMgrPanel::onEditBtnClicked);
	hLayout->addWidget(addBtn);
	hLayout->addWidget(delBtn);
	hLayout->addWidget(editBtn);
	hLayout->addStretch(1);
	mainLayout->addLayout(hLayout);

	m_glWidget = new GlWidget();
	mainLayout->addWidget(m_glWidget);
}

surfaceMgrPanel::~surfaceMgrPanel()
{
	delete m_glWidget;
}

static int static_x = 0;
static int static_y = 0;
void surfaceMgrPanel::onAddBtnClicked()
{
	m_glWidget->addSurface()->setPoint(QPoint(static_x, static_y));
	static_x += 10;
	static_y += 10;
}

void surfaceMgrPanel::onDelBtnClicked()
{
	for (Surface* s : m_glWidget->getSurfaces())
	{
		if (s->getSelect())
		{
			m_glWidget->removeSurface(s);
		}
	}
}

void surfaceMgrPanel::onEditBtnClicked()
{
	for (Surface* s : m_glWidget->getSurfaces())
	{
		if (s->getSelect())
		{
			SurfaceEditDlg dlg(s);
			dlg.exec();
			break;
		}
	}
}

SurfaceEditDlg::SurfaceEditDlg(Surface* s, QWidget* parent) : QDialog(parent), m_surface(s)
{
	QVBoxLayout* mainLayout = new QVBoxLayout(this);

	{
		QLabel* pathLabel = new QLabel(this);
		pathLabel->setText("path");
		m_pathEdit = new QLineEdit(this);
		QHBoxLayout* hLayout = new QHBoxLayout();
		hLayout->setContentsMargins(0, 0, 0, 0);
		hLayout->addWidget(pathLabel);
		hLayout->addWidget(m_pathEdit);
		mainLayout->addLayout(hLayout);
	}
	mainLayout->addStretch(1);
	{
		QPushButton* okBtn = new QPushButton("ok");
		connect(okBtn, &QPushButton::clicked, this, &SurfaceEditDlg::onOkBtnClicked);
		QPushButton* cancelBtn = new QPushButton("cancel");
		connect(cancelBtn, &QPushButton::clicked, this, &SurfaceEditDlg::onCancelBtnClicked);
		QHBoxLayout* hLayout = new QHBoxLayout();
		hLayout->setContentsMargins(0, 0, 0, 0);
		hLayout->addStretch(1);
		hLayout->addWidget(okBtn);
		hLayout->addWidget(cancelBtn);
		mainLayout->addLayout(hLayout);
	}
}

SurfaceEditDlg::~SurfaceEditDlg()
{

}

void SurfaceEditDlg::onOkBtnClicked()
{
	m_surface->setImage(QImage(m_pathEdit->text()));
	QDialog::accept();
}

void SurfaceEditDlg::onCancelBtnClicked()
{
	QDialog::reject();
}
